![]() |
PATH![]() |
![]() ![]() |
The Count command can function as an AppleScript command or an application command. The AppleScript command counts the number of elements of a particular class in a list, record, or string. The application command counts the number of elements of a particular class in an object or objects.
count [ [ each | every ] className | pluralClassName ( in | of ) ] compositeValue
number of [ className | pluralClassName ( in | of ) ] compositeValue
count [ each | every ] className | pluralClassName [ ( in | of ) referenceToObject ]
number of className | pluralClassName [ ( in | of ) referenceToObject ]
The result of the AppleScript command is an integer that specifies the number of elements of a specified class in a composite value.
The result of the application command is either an integer or a list of integers. See "Notes" for details.
In the following example, compositeValue is a list. The command does not explicitly specify a class of elements to count, so AppleScript counts all the items in the list.
count {"Yes", "No", "Maybe", 4, 5, 6}
--result: 6
In this example, className is integers and referenceToObject is a list of strings and integers. The following statements count first the integers in the list, then the strings:
count the integers in {"Yes", "No", "Maybe", 4, 5, 6}
--result: 3
count the strings in {"Yes", "No", "Maybe", 4, 5, 6}
--result: 3
This example shows another way to count the integers in the list:
count each integer in {"Yes", "No", "Maybe", 4, 5, 6}
--result: 3
You can use the term number of as well to count items in a list:
number of integers in {"Yes", "No", "Maybe", 4, 5, 6}
--result: 3
In the following example, every file of disk 1 evaluates to a list of files. The Finder counts the files in the list.
tell application "Finder"
count every file of disk 1
end tell
--result: number of files on first disk
The following statement is equivalent to the previous example:
tell application "Finder"
count files of disk 1
end tell
In the following example, referenceToObject is windows of application "Finder", which is a list of windows. The Finder counts the windows in the list.
count of windows of application "Finder"
If you use the Count command on a string without specifying the class to be counted, AppleScript counts the characters. Consider the following two examples.
count "This is a string"
--result: 16
count words in "This is a string"
--result: 4
If you count a list or record without specifying a class, the Count command counts the items:
count {1, 2, 5, 3} --result: 4
count {name: Jun, height: 72, weight: 200} --result: 3